<?php

namespace app\admin\command;
set_time_limit(0);


use app\common\model\Demand;
use app\common\model\Goods;
use app\common\model\Shop;
use app\common\model\User;
use think\console\Command;
use think\console\Input;
use think\console\input\Option;
use think\console\Output;
use think\Log;

/**
 * Class Factory
 * @package app\admin\command
 */
class Factory extends Command
{
    protected function configure()
    {
        $this
            ->setName('factory')
            ->addOption('type', 't', Option::VALUE_REQUIRED, '类型', null)
            ->addOption('amount', 'a', Option::VALUE_REQUIRED, '数量', null)
            ->addOption('delete', 'd', Option::VALUE_OPTIONAL, '删除', false)
            ->setDescription('批量生成数据');
    }

    /**
     * @param Input $input
     * @param Output $output
     * @return int|void|null
     */
    protected function execute(Input $input, Output $output)
    {
        $type = $input->getOption('type');
        $amount = $input->getOption('amount');
        $delete = $input->getOption('delete');
        $info = self::$type($amount,$delete);
        $output->info($info);
    }


    /**
     * 填充用户数据
     * @param $quantity
     * @return string
     * @throws \Exception
     */
    public static function user($quantity, $delete = false)
    {
        if ($delete){
            self::demand(1, true);
            self::goods(1, true);
            User::destroy(['bio'=> 'hello-master']);
            return '清理用户及订单完毕';
        }
        $faker = \Faker\Factory::create('zh_CN');
        $data = [];
        for ($i=0; $i< $quantity; $i++){
            $data[$i]['group_id'] = 1;
            $data[$i]['username'] = $data[$i]['nickname'] = 'ts'.$faker->numberBetween($min = 1000000, $max = 9999999);
            $data[$i]['salt'] = $faker->regexify('[A-Z0-9]{7}');
            $data[$i]['password'] = md5(md5('ts123456').$data[$i]['salt']);
            $data[$i]['bio'] = 'hello-master';
            $data[$i]['status'] = 'normal';
        }
        $user = new User();
        $user->saveAll($data);
        return '共添加了'.$quantity.'条用户记录';
    }

    /**
     * 填充需求数据
     * @param $quantity
     * @param false $delete
     * @return string
     * @throws \think\exception\DbException
     */
    public static function demand($quantity, $delete = false)
    {
        $users = collection(User::all(['bio'=> 'hello-master']))->toArray();
        if (empty($users)){
            if ($delete){
                return '无测试用户数据';
            } else{
                return '暂无相关用户，请先添加';
            }
        }
        if ($delete){
            $ids = implode(',', array_column($users,'id'));
            Demand::where('user_id','in', $ids)->delete();
            return '删除需求成功';
        }

        $faker = \Faker\Factory::create('zh_CN');

        $succ = 0;
        $fail = 0;
        for ($i=0; $i< $quantity; $i++){
            $user_i = rand(0, count($users)-1);
            $user_id = $users[$user_i]['id'];

            $data['user_id'] = $user_id;
            $data['title'] = $faker->text(50);
            $data['budget'] = rand(100,10000);
            $data['mobile'] = $faker->phoneNumber();
            $data['describe'] = $faker->text();
            $data['cover'] = 'http://oss.zhidebang.lexianglvpai.com/uploads/20210129/1d28448d2298e87ab4db6abec7c8aae3.jpg';
            $data['onshelf_status'] = 1;
            $data['verify_status'] = 'allow';

            $res = Demand::create($data);
            if ($res){
                echo '需求id：'.$res->id."\n";
                $succ++;
            } else {
                Log::info('失败数据');
                Log::info($data);
                echo '需求新增失败'."\n";
                $fail++;
            }
        }
        return '成功'.$succ.'条，失败'.$fail.'条';
    }

    /**
     * 填充服务数据
     * @param $quantity
     * @param false $delete
     * @return string
     * @throws \think\exception\DbException
     */
    public static function goods($quantity, $delete = false)
    {
        $users = collection(User::all(['bio'=> 'hello-master']))->toArray();
        if (empty($users)){
            if ($delete){
                return '无测试用户数据';
            } else{
                return '暂无相关用户，请先添加';
            }
        }

        if ($delete){
            $ids = implode(',', array_column($users,'id'));
            Goods::where('user_id','in', $ids)->delete();
            return '删除服务成功';
        }


        $faker = \Faker\Factory::create('zh_CN');

        $succ = 0;
        $fail = 0;
        for ($i=0; $i< $quantity; $i++){
            $user_i = rand(0, count($users)-1);
            $user_id = $users[$user_i]['id'];
            $shop = Shop::getByUserId($user_id);

            $data['user_id'] = $user_id;
            $data['shop_id'] = $shop->id;
            $data['title'] = $faker->text(50);
            $data['cover'] = 'http://oss.zhidebang.lexianglvpai.com/uploads/20210127/1a78add5ba6d919e171a248db4c8c7c5.jpg';
            $data['price'] = rand(100,10000);
            $data['unit'] = '次';
            $data['describe'] = $faker->text();
            $data['images'] = json_encode(['http://oss.zhidebang.lexianglvpai.com/uploads/20210127/9f61b88f5862743cd7ed41b660118f6b.jpg','http://zhidebang.lexianglvpai.com/uploads/20210123/77351aceefb7c686c7f52e79cf2a5691.jpg']);
            $data['onshelf_status'] = 1;
            $data['verify_status'] = 'allow';

            $res = Goods::create($data);
            if ($res){
                echo '服务id：'.$res->id."\n";
                $succ++;
            } else {
                Log::info('失败数据');
                Log::info($data);
                echo '服务新增失败'."\n";
                $fail++;
            }
        }
        return '成功'.$succ.'条，失败'.$fail.'条';
    }
}